/*
 * Copyright (c) 2022-2022 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "los_mnt_container.h"
#include "los_config.h"
#include "vnode.h"

#ifdef LOSCFG_MNT_CONTAINER
extern int umount(const char *target);
extern struct ContainerConfig g_containerConfig;

UINT32 g_currentMntContainerNum = 1;
MntContainer g_rootMntContainer;

static MntContainer *CreateMntContainer()
{
    if (g_currentMntContainerNum > g_containerConfig.MaxMntContainer)
    {
        PRINT_ERR("CreateMntContainer failed, reach to max container config.\n");
        return NULL;
    }

    MntContainer *mntContainer = NULL;
    UINT32 size = sizeof(MntContainer);
    mntContainer = LOS_MemAlloc(m_aucSysMem1, size);

    if (mntContainer == NULL) {
        PRINT_ERR("CreateMntContainer failed to malloc memory.\n");
        return NULL;
    }

    UINT32 intSave;
    SCHEDULER_LOCK(intSave);
    g_currentMntContainerNum += 1;
    SCHEDULER_UNLOCK(intSave);

    atomic_set(&mntContainer->count, 1);
    mntContainer->mountList = NULL;
    return mntContainer;
}

static inline bool InitMountList(MntContainer* mntContainer)
{
    if (mntContainer->mountList == NULL) {
        mntContainer->mountList = zalloc(sizeof(LIST_HEAD));
        if (mntContainer->mountList == NULL) {
            PRINT_ERR("init mount list failed, no memory.");
            return false;
        }
        LOS_ListInit(mntContainer->mountList);
    }

    return true;
}

static MntContainer *CreateNewMntContainer(MntContainer *runMntContainer)
{
    MntContainer *newMntContainer = NULL;
    newMntContainer = CreateMntContainer();

    if (newMntContainer == NULL) {
        PRINT_ERR("newMntContainer is NULL.\n");
        return NULL;
    }

    if (!InitMountList(newMntContainer))
    {
        return NULL;
    }

    struct Mount *mnt = NULL;
    LOS_DL_LIST_FOR_EACH_ENTRY(mnt, runMntContainer->mountList, struct Mount, mountList) {
        struct Mount *newMnt = (struct Mount*)zalloc(sizeof(struct Mount));
        if (newMnt == NULL) {
            PRINT_ERR("New mount alloc failed no memory!\n");
            return NULL;
        }
        *newMnt = *mnt;
        LOS_ListTailInsert(newMntContainer->mountList, &(newMnt->mountList));
        atomic_inc(&mnt->vnodeCovered->mntCount);
    }

    ContainerAllocVnum(&(newMntContainer->containerBase.vnum));
    return newMntContainer;
}

/*
 * Share the parent process MntContainer or create a new MntContainer
 */
MntContainer *HandleMntContainer(unsigned long flags, MntContainer *runMntContainer)
{
    MntContainer *newMntContainer = NULL;

    if (!(flags & CLONE_NEWNS)) {
        GetMntContainer(runMntContainer);
        return runMntContainer;
    }

    newMntContainer = CreateNewMntContainer(runMntContainer);
    if (newMntContainer == NULL) {
        PRINT_ERR("newMntContainer is NULL.\n");
        return NULL;
    }

    return newMntContainer;
}

void FreeMntContainer(MntContainer *mntContainer)
{
    struct Mount *mnt = NULL;
    struct Mount *nextMnt = NULL;
    LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(mnt, nextMnt, mntContainer->mountList, struct Mount, mountList) {
        if (mnt->vnodeCovered->mntCount > 0) {
            mnt->vnodeCovered->mntCount--;
            LOS_ListDelete(&mnt->mountList);
            free(mnt);
        } else {
            umount(mnt->pathName);
        }
    }
    free(mntContainer->mountList);

    LOS_MemFree(m_aucSysMem1, mntContainer);

    UINT32 intSave;
    SCHEDULER_LOCK(intSave);
    g_currentMntContainerNum -= 1;
    SCHEDULER_UNLOCK(intSave);
}

INT32 ReplaceMntContainerInBundle(ContainerBundle* bundle, struct MntContainer *targetMntContainer)
{
    if (bundle->mntContainer) {
        DerefMntContainer(bundle->mntContainer);
    }

    bundle->mntContainer = targetMntContainer;
    GetMntContainer(targetMntContainer);

    return LOS_OK;
}

void InitMntContainer(MntContainer *mntContainer)
{
    mntContainer->count = ATOMIC_INIT(1);

    if (!InitMountList(mntContainer)) {
        return;
    }

    ContainerAllocVnum(&(mntContainer->containerBase.vnum));

    return;
}
#endif